//
//  ViewController.swift
//  Calcullator
//
//  Created by gaoyi on 2017/9/26.
//  Copyright © 2017年 gaoyi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var display: UILabel!
    
    var userIsInTheMiddleOfTypingANumber = false
    @IBAction func appendDigit(_ sender: UIButton) {
        let digit = sender.currentTitle!
        display.text = display.text! + digit
        print("digit = \(String(describing: digit))" as Any)
        if userIsInTheMiddleOfTypingANumber{
            display.text = display.text! + digit
            
        }else{
            display.text = digit
            userIsInTheMiddleOfTypingANumber = true
        }
    }
    @IBAction func operate(_ sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingANumber{
            enter()
        }
        switch operation{
        case "×":performOperation {$0 * $1}
        case "÷":performOperation {$1 / $0}
        case "+":performOperation {$0 + $1}
        case "−":performOperation {$1 * $0}
        case "√":performOperationsqrt{sqrt($0)}
        default:break
        }
    }
    func performOperation(operation : (Double, Double) -> Double)
    {
        if operandStack.count>=2{
            displayValue = operation(operandStack.removeLast(),operandStack.removeLast())
            enter()
        }
    }
    func performOperationsqrt(operation : (Double) -> Double)
    {
        if operandStack.count>=1{
            displayValue = operation(operandStack.removeLast())
            enter()
        }
    }
    var operandStack = Array<Double>()
    @IBAction func enter() {
        userIsInTheMiddleOfTypingANumber = false
        operandStack.append(displayValue)
        print("operandStack = \(operandStack)")
    }
    
    var displayValue: Double{
        get{
            
            return NumberFormatter().number(from: display.text!)!.doubleValue
        }
        set{
            display.text = "\(newValue)"
            userIsInTheMiddleOfTypingANumber = false
        }
    }
    
    
}


